package com.ch.consumer.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @author cj-ch
 * @date 2018/4/21 下午2:47
 */
@Service
public class HystrixTestService {

    final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "hystrixTestFallback")
    public String hystrixTest(){
        long start = System.currentTimeMillis();
        ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://produce/hystrix-test", String.class);
        long end = System.currentTimeMillis();
        logger.info("用时:"+(end - start));
        return responseEntity.getBody();
    }

    /**
     * 服务降级
     * @return
     */
    String hystrixTestFallback(){
        return "hystrixTest服务降级处理";
    }
}
